<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_block_PageEditAction extends m_backoffice_extends_Action
{
	public function execute()
	{	
		$documentId = $this->getRequestParameter("docId");
		$documentLang = $this->getRequestParameter("docLang");
	
		$tempDocService = m_backoffice_services_TempDocService::getInstance();

		$page = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Page', $documentId, $documentLang);	

		if($page->getPublicationstatus() == "NOT TRANSLATED")
		{
			return $this->launchAction("website", "block_TranslatePage");
		}

		$tabAction = $this->getBlockContext()->getParameter("tabName");

		if(is_null($tabAction))
		{
            $tabAction = "tabPageStructure";
        }

        $userContext = $this->getUserContext();
        if(!$userContext->hasRight(array("website_TabPageStructure", "website_BlockEdit", "website_ValidatePage"), "OR"))
        {
            $tabAction = "tabPageProperties";
        }

        $model['tab'] = '{block module="website" action="'.$tabAction.'" documentId="'.$documentId.'" documentLang="'.$documentLang.'"}';
        $model['tabName'] = $tabAction;
					
		$model["style"] = "";
		
		if($tempDocService->hasTemporary($documentId, $documentLang))
		{
			$model["tempPage"] = $tempDocService->getTemporaryDocument($documentId, $documentLang);
		}		
		
		$model['page'] = $page;
	
		$revisions = $page->getRevisions();

		if(isset($revisions[0]))
		{
			array_pop($revisions);
			krsort($revisions);
			
			foreach($revisions as $revisionId => $values)
			{
				if($model['page']->getLastRevisionNumber() == $revisionId+1)
				{
					unset($revisions[$revisionId]);
				}
			}
		}
		
		$model['revisions'] = $revisions;
		
		$this->setModel($model);
		
		$this->setView('success');
	}	
	
	public function validate()
	{
		return $this->getUserContext()->hasRight(array("website_TabPageProperties","website_TabPageStructure"), "OR");
	}
}